package com.ytzl.itrip.utils.common;

import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
 * Created by asus on 2018/7/30.
 */
@Component
public class ScoreUtils {
    public Integer score(int[] n) {
        // 设置中间数用来比较
        int m = 0;
// 排序，升序
        Arrays.sort(n);
// 保存结果map，n的元素为key，出现次数为value
        Map nums = new HashMap();
// 首先中间数出现次数为0
        nums.put(m, 0);
// 循环，当某个数已经在map里时，将次数加一
        for (int i = 0; i < n.length; i++) {
            if (m == n[i]) {
                int v = (Integer) nums.get(m);
                v++;
                nums.put(m, v);
            } else {
                nums.put(n[i], 1);
            }
            m = n[i];
        }
// 遍历得到最多次数的数
        Iterator iterator = nums.keySet().iterator();
        int maxNumber = 0;
        int maxValue = 0;
        while (iterator.hasNext()) {
            int key = (Integer) iterator.next();
            int value = (Integer) nums.get(key);
            if (value > maxNumber) {
                maxNumber = value;
                maxValue = key;
            }
        }
        System.out.println("出现次数最多的数为：" + maxValue + ",出现次数为：" + maxNumber);
        return maxValue;
    }
}
